﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Waiyun.WebApi
{
    public class ImportationSDController : ApiController
    {
        public static string ConnectionString;

        static ImportationSDController()
        {
            ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["waiyun_v2"].ConnectionString;
        }

        /// <summary>
        /// 委托
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Delegate()
        {
            string name = "ImportationSD\\Delegate";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1)
  from OD_F_STATS od
 where od.d_order_time > sysdate - 15
   and od.n_accept_num > 0
   and od.v_org_id IN (select c.v_org_id
                      from PRD_DW.DF_SINO_COMPANY c
                     where c.v_secon_org = '山东'
                       and c.v_is_order = 'Y')", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }
    
        /// <summary>
        /// 卸船
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Unloading()
        {
            string name = "ImportationSD\\Unloading";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1)
  from OD_F_STATS o
 where o.N_OCGAT_NUM = 1
   and o.N_LOADING_NUM = 0
   and o.d_order_time > sysdate - 15
   and o.v_org_id IN (select c.v_org_id
                        from PRD_DW.DF_SINO_COMPANY c
                       where c.v_secon_org = '山东'
                         and c.v_is_order = 'Y')", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }
        /// <summary>
        /// 通关
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Clearance()
        {
            string name = "ImportationSD\\Clearance";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1)
  from OD_F_STATS o
 where o.N_CTDCL_NUM = 1
   and o.N_CUST_CLER_NUM = 0
   and o.d_order_time > sysdate - 15
   and o.v_org_id IN (select c.v_org_id
                        from PRD_DW.DF_SINO_COMPANY c
                       where c.v_secon_org = '山东'
                         and c.v_is_order = 'Y')", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }

        /// <summary>
        /// 空箱返场
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> EmptyBack()
        {
            string name = "ImportationSD\\EmptyBack";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1)
  from OD_F_STATS o
 where o.N_LOADING_NUM = 1
   and o.N_DISCHARGE_NUM = 0
   and o.d_order_time > sysdate - 15
   and o.v_org_id IN (select c.v_org_id
                        from PRD_DW.DF_SINO_COMPANY c
                       where c.v_secon_org = '山东'
                         and c.v_is_order = 'Y')", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }
    }
}
